package com.xinzhidi.web.equipment.controller;

import com.xinzhidi.common.annotation.Log;
import com.xinzhidi.common.core.controller.BaseController;
import com.xinzhidi.common.core.domain.AjaxResult;
import com.xinzhidi.common.core.domain.R;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.common.enums.BusinessType;
import com.xinzhidi.common.utils.poi.ExcelUtil;
import com.xinzhidi.web.equipment.dto.*;
import com.xinzhidi.web.equipment.service.IEquipmentArchivesService;
import com.xinzhidi.web.equipment.vo.EquipmentArchivesInfoVo;
import com.xinzhidi.web.equipment.vo.EquipmentArchivesListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 存档Controller
 *
 * @author cx
 * @date 2023-07-12
 */
@RestController
@Api(tags = "存档")
@RequestMapping("/archives")
public class EquipmentArchivesController extends BaseController {
    @Autowired
    private IEquipmentArchivesService equipmentArchivesService;

    @ApiOperation("查询存档列表")
    @PreAuthorize("@ss.hasPermi('equipment:archives:list')")
    @PostMapping("/listEquipmentArchives")
    public TableDataInfo<EquipmentArchivesListVo> listEquipmentArchives(@Validated @RequestBody EquipmentArchivesListDto dto) {
        startPagePost(dto);
        return equipmentArchivesService.selectEquipmentArchivesList(dto);
    }

    @ApiOperation("导出存档列表")
    @PreAuthorize("@ss.hasPermi('equipment:archives:export')")
    @Log(title = "存档", businessType = BusinessType.EXPORT)
    @PostMapping("/exportEquipmentArchives")
    public void exportEquipmentArchives(HttpServletResponse response, EquipmentArchivesListDto dto) {
        TableDataInfo<EquipmentArchivesListVo> list = equipmentArchivesService.selectEquipmentArchivesList(dto);
        ExcelUtil<EquipmentArchivesListVo> util = new ExcelUtil<EquipmentArchivesListVo>(EquipmentArchivesListVo.class);
        util.exportExcel(response, list.getRows(), "存档数据");
    }

    @ApiOperation("获取存档详细信息")
    @PreAuthorize("@ss.hasPermi('equipment:archives:query')")
    @GetMapping(value = "/getEquipmentArchivesById")
    public R<EquipmentArchivesInfoVo> getEquipmentArchivesById(@RequestParam("id") Long id) {
        return R.ok(equipmentArchivesService.selectEquipmentArchivesById(id));
    }

    @ApiOperation("新增存档")
    @PreAuthorize("@ss.hasPermi('equipment:archives:add')")
    @Log(title = "存档", businessType = BusinessType.INSERT)
    @PostMapping("addEquipmentArchives")
    public AjaxResult addEquipmentArchives(@Validated @RequestBody EquipmentArchivesAddDto dto) {
        return toAjax(equipmentArchivesService.insertEquipmentArchives(dto));
    }

    @ApiOperation("批量新增存档")
    @PreAuthorize("@ss.hasPermi('equipment:archives:add')")
    @Log(title = "批量新增存档", businessType = BusinessType.INSERT)
    @PostMapping("addEquipmentArchivesList")
    public AjaxResult addEquipmentArchivesList(@Validated @RequestBody EquipmentArchivesAddListDto dto) {
        return toAjax(equipmentArchivesService.insertEquipmentArchivesList(dto));
    }

    @ApiOperation("修改存档")
    @PreAuthorize("@ss.hasPermi('equipment:archives:edit')")
    @Log(title = "存档", businessType = BusinessType.UPDATE)
    @PostMapping("editEquipmentArchives")
    public AjaxResult editEquipmentArchives(@Validated @RequestBody EquipmentArchivesUpdDto dto) {
        return toAjax(equipmentArchivesService.updateEquipmentArchives(dto));
    }

    @ApiOperation("删除存档")
    @PreAuthorize("@ss.hasPermi('equipment:archives:remove')")
    @Log(title = "存档", businessType = BusinessType.DELETE)
    @DeleteMapping("/delEquipmentArchives")
    public AjaxResult delEquipmentArchives(@RequestParam Long[] ids) {
        return toAjax(equipmentArchivesService.deleteEquipmentArchivesByIds(ids));
    }

    @ApiOperation("恢复存档")
    @PostMapping("/recoverEquipmentArchives")
    public AjaxResult recoverEquipmentArchives(@RequestBody List<recoverDto> dtoList){
        equipmentArchivesService.recoverEquipmentArchives(dtoList);
        return AjaxResult.success();
    }

}
